package com.zemo.springmvc.control;

import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.zemo.springmvc.bean.User;
import com.zemo.springmvc.bean.UserInfo;
import com.zemo.springmvc.service.UserInfoService;
import com.zemo.springmvc.service.UserService;

@Controller
@RequestMapping(value="/user")
public class UserAction {
	@Autowired
	private UserService userService;
	@Autowired
	private UserInfoService userInfoService;
	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	public String addUser(@RequestParam(value="userName",required=true)String userName,
							@RequestParam(value="userSex",required=true)String userSex,
							@RequestParam(value="password",required=true)String password,
							@RequestParam(value="name",required=true)String name,
							@RequestParam(value="address")String address){
		User user = new User();
		UserInfo userInfo = new UserInfo();
		user.setUserName(userName);
		user.setUserSex(userSex);
		user.setPassword(password);
		user.setUserInfo(userInfo);
		userInfo.setName(name);
		userInfo.setAddress(address);
		userInfo.setUser(user);
		
		return this.userService.addUser(user)==true ?"forward:userList":"fail";
	}
	@RequestMapping(value="/userList",method=RequestMethod.POST)
	public String getAllUser(HttpServletRequest request){
		List<User> users = userService.getUserList();
		request.setAttribute("users", users);
		return "userList";
	}
	@RequestMapping(value="/userInfo",method=RequestMethod.GET)
	public String getUserInfoById(HttpServletRequest request,Integer userId){
		UserInfo userInfo = userInfoService.getUserInfoById(userId);
		request.setAttribute("userInfo",userInfo);
		return "userInfo";
	}
}
